package com.lepu.app.fun.chat.core;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;

/**
 * Created by Chang on 12/22/2014.
 */
public class MediaPacket extends Packet {
    PacketExtension extension;
    Message message;

    public MediaPacket() {
    }

    public MediaPacket(Message message) {
        this.message = message;
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public PacketExtension getExtension() {
        return extension;
    }

    public void setExtension(PacketExtension extension) {
        this.extension = extension;
    }

    @Override
    public CharSequence toXML() {
        if (extension == null) {
            return message.toXML();
        } else {
            String pid = this.message.getPacketID();
            String toId = this.message.getTo();
            String fromId = this.message.getFrom();
            this.setTo(toId);
            this.setFrom(fromId);
            //String fromId = this.getFrom();

            StringBuffer sb = new StringBuffer();
            sb.append("<message id=\"").append(pid);
            sb.append("\" to=\"").append(toId);
            sb.append("\" from=\"").append(fromId);
            sb.append("\" type=\"").append("chat").append("\">");
            sb.append(extension.toXML());
            sb.append("<request xmlns=\"urn:xmpp:receipts\"/>");
            sb.append("</message>");

            CharSequence charSequence = new StringBuffer(sb.toString());
            return charSequence;

        }
    }
}
